//
//  AccountManager.m
//  EasyToo
//
//  Created by zhengzhe on 13-8-24.
//
//

#import "AccountManager.h"
#import "LoginRequest.h"
#import "LoginResponse.h"
#import "ServerCommunicationManager.h"
#import "CheckAccountInfoRequest.h"
#import "VerifySMSRequest.h"
#import "ModifyPhoneNumRequest.h"
#import "ModifyIDCardRequest.h"
#import "DeviceUtil.h"

#import "SendSMSCheckVerifyCodeRequest.h"
#import "SendSMSCheckVerifyCodeResponse.h"

#import "RegisterRequest.h"
#import "RegisterResponse.h"

#import "ResetPasswdRequest.h"
#import "ResetPasswdResponse.h"

#import "SendSMSCodeToGetPassRequest.h"
#import "SendSMSCodeToGetPassResponse.h"


@implementation AccountManager

static AccountManager* gManager = nil;

+ (AccountManager *)shareManager
{
    if (gManager == nil) {
        gManager = [[AccountManager alloc] init];
    }
    return gManager;
}

- (void)getAccountInfo:(NSString *)account
{
    CheckAccountInfoRequest *request = [[CheckAccountInfoRequest alloc] initWithCardId:account Delegate:self];
    [[ServerCommunicationManager GetServerCommunication] sendRequest:request];
}

//请求验证码
- (void)registerSendVerifyCode:(NSString *)account
{
    SendSMSCheckVerifyCodeRequest *request = [[SendSMSCheckVerifyCodeRequest alloc] initWithPhoneNum:account Delegate:self];
    [[ServerCommunicationManager GetServerCommunication] sendRequest:request];
}

//找回密码
- (void)getPassBySendSMSCode:(NSString*)account
{
    SendSMSCodeToGetPassRequest *request = [[SendSMSCodeToGetPassRequest alloc] initWithPhoneNum:account Delegate:self];
    [[ServerCommunicationManager GetServerCommunication] sendRequest:request];

}

//用户注册
- (void)userRegister:(NSString*)telephone password:(NSString*)password cardId:(NSString*)cardId
{
    NSString *md5pw = [DeviceUtil getMD5:password];
    RegisterRequest *request = [[RegisterRequest alloc] initWithTelephone:telephone
                                                                 Password:md5pw
                                                                   CardId:cardId
                                                                 Delegate:self];
    [[ServerCommunicationManager GetServerCommunication] sendRequest:request];

}


//重置密码
- (void)resetPasswd:(NSString*)telephone
           password:(NSString*)password
             cardId:(NSString*)cardId
            apiCode:(NSString*)apiCode
{
    NSString *md5pw = [DeviceUtil getMD5:password];
    ResetPasswdRequest *request = [[ResetPasswdRequest alloc] initWithTelephone:telephone
                                                                       Password:md5pw
                                                                         CardId:cardId
                                                                        apiCode:apiCode
                                                                       Delegate:self];
    [[ServerCommunicationManager GetServerCommunication] sendRequest:request];
}

//用户登录
- (void)login:(NSString *)account Password:(NSString *)password
{
    NSString *md5pw = [DeviceUtil getMD5:password];
    LoginRequest *request = [[LoginRequest alloc] initWithAccount:account Password:md5pw Delegate:self];
    [[ServerCommunicationManager GetServerCommunication] sendRequest:request];
}

- (void)registerVerify:(NSString *)account Code:(NSString *)code
{
    VerifySMSRequest *request = [[VerifySMSRequest alloc] initWithCheckCode:code CardId:account Request:@"double" Delegate:self];
    [[ServerCommunicationManager GetServerCommunication] sendRequest:request];
}



- (void)onRequestSuccess:(BaseResponse *)response
{
    //登录成功
    if ([response.request isKindOfClass:[LoginRequest class]])
    {
        [self handleUserLogin:(LoginResponse*)response];
    }
    
    //请求验证码
    else if ([response.request isKindOfClass:[SendSMSCheckVerifyCodeRequest class]])
    {
        [self handleSendSMSCode:(SendSMSCheckVerifyCodeResponse*)response];
    }
    
    //找回密码
    else if ([response.request isKindOfClass:[SendSMSCodeToGetPassRequest class]])
    {
        [self.delegate getPassWd:YES];
    }
    
    //用户注册
    else if ([response.request isKindOfClass:[RegisterRequest class]])
    {
        [self handleRegisterLogin:(RegisterResponse*)response];
    }
    
    //重置密码
    else if ([response.request isKindOfClass:[ResetPasswdRequest class]])
    {
        [self handleRetsetPasswd:(ResetPasswdResponse*)response];
    }
    
}

- (void)onRequestFail:(BaseResponse *)response
{
    //登陆失败
    if ([response.request isKindOfClass:[LoginRequest class]])
    {
        [self.delegate loginUser:NO accstatus:nil];
    }
    
    //请求验证码
    else if ([response.request isKindOfClass:[SendSMSCheckVerifyCodeRequest class]])
    {
        [self.delegate sendSMSCode:NO checkCode:nil];
    }
    
    //找回密码
    else if ([response.request isKindOfClass:[SendSMSCodeToGetPassRequest class]])
    {
        [self.delegate getPassWd:NO];
    }
    
    //用户注册
    else if ([response.request isKindOfClass:[RegisterRequest class]])
    {
        [self.delegate registerUser:NO accstatus:nil];
    }
    
    //重置密码
    else if ([response.request isKindOfClass:[ResetPasswdRequest class]])
    {
        [self.delegate resetPasswd:NO point:nil];
    }
}



//请求验证码
- (void)handleSendSMSCode:(SendSMSCheckVerifyCodeResponse*)response
{
    NSString *checkCode = response.checkCode;
    if (checkCode)
    {
        [self.delegate sendSMSCode:YES checkCode:checkCode];
    }
    else
    {
        [self.delegate sendSMSCode:NO checkCode:nil];
    }
}

//用户登录
- (void)handleUserLogin:(LoginResponse*)response
{
    NSString *accstatus = response.accstatus;
    if (accstatus)
    {
        [self.delegate loginUser:YES accstatus:accstatus];
    }
    else
    {
        [self.delegate loginUser:NO accstatus:nil];
    }
}

//用户注册
- (void)handleRegisterLogin:(RegisterResponse*)response
{
    NSString *accstatus = response.accstatus;
    if (accstatus)
    {
        [self.delegate registerUser:YES accstatus:accstatus];
    }
    else
    {
        [self.delegate registerUser:NO accstatus:nil];
    }
}

//重置密码
- (void)handleRetsetPasswd:(ResetPasswdResponse*)response
{
    NSString *msg = response.msg;
    if ([msg isEqualToString:@"成功"])
    {
        [self.delegate resetPasswd:YES point:msg];
    }
    else
    {
        [self.delegate resetPasswd:NO point:nil];
    }
}

@end
